package com.kzw.oa.comm.utils;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.jxls.transformer.XLSTransformer;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class ExportUtils {
	public static void exportAllXls(String pathname,String fileName, List<Map<String,Object>> rows, String deptname, HttpSession session,HttpServletResponse response){
		// 创建数据模型
		Map<String, Object> root = new HashMap<String, Object>();
		root.put("rows", rows);
		root.put("deptname", deptname);
		try {
			String path = session.getServletContext().getRealPath("/pages/excel/"+ pathname);
			InputStream is = new FileInputStream(path);
			HSSFWorkbook workBook = new HSSFWorkbook(is);
			 //关联模板   
			XLSTransformer transformer = new XLSTransformer(); 
			transformer.transformWorkbook(workBook, root);  
	
			// 将模板和数据模型合并生成文件
			OutputStream out = response.getOutputStream();
			response.resetBuffer();
			String now = new SimpleDateFormat("yyyyMMdd").format(new Date());
			fileName = fileName+now+".xls";
			fileName = new String(fileName.getBytes("GBK"), "ISO8859-1");
			response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
			response.setContentType("application/octet-stream");
	
			workBook.write(out);
			out.close();
		} catch(Exception e) {
			e.printStackTrace();
		}
	}
		
}
